﻿Imports Controle
Public Class frmPesquisa

#Region "Variáveis"
    Private objPesquisa As New ctrPesquisa
    Private prvCampos As String
    Private prvDescricoes As String
    Private prvLarguras As String
    Private prvDtSet As DataSet
    Private prvTabela As DataTable
    Private prvSql As String
    Private prvId As Long
    Private prvTitulo As String
    Private dsPesquisa As New DataSet
    Private prvCampoSelecao As Int32 = 1

#End Region

#Region "Propriedades"
    Public Property DS() As DataSet
        Get
            Return Me.dsPesquisa
        End Get
        Set(ByVal value As DataSet)
            Me.dsPesquisa = value
        End Set
    End Property

    Public WriteOnly Property Campos()
        Set(ByVal value)
            prvCampos = value
        End Set
    End Property
    Public WriteOnly Property Descricoes()
        Set(ByVal value)
            prvDescricoes = value
        End Set
    End Property
    Public WriteOnly Property Larguras()
        Set(ByVal value)
            prvLarguras = value
        End Set
    End Property
    Public WriteOnly Property Sql()
        Set(ByVal value)
            prvSql = value
        End Set
    End Property
    Public Property Id()
        Get
            Return prvId
        End Get
        Set(ByVal value)
            prvId = value
        End Set
    End Property
    Public Property Titulo()
        Get
            Return prvTitulo
        End Get
        Set(ByVal value)
            prvTitulo = value
        End Set
    End Property
    Public Property CampoSelecao()
        Get
            Return prvCampoSelecao
        End Get
        Set(ByVal value)
            prvCampoSelecao = value
        End Set
    End Property

#End Region

#Region "Funções"

    Private Sub CarregarCboCampos()
        Dim Itens() As System.Object

        Try
            ReDim Itens(UBound(Split(prvCampos, ",")))
            With cboCampos
                For cont As Integer = 0 To UBound(Split(prvCampos, ","))
                    Itens(cont) = Mid(Split(prvDescricoes, ",")(cont).ToString, 1)
                Next
                .Items.AddRange(Itens)
                .SelectedIndex = prvCampoSelecao

            End With

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, Me.Text)
        End Try

    End Sub
    Public Sub CarregaTela()
        DS.Clear()
        DS = Me.objPesquisa.Pesquisa(prvSql)
        If Not DS Is Nothing AndAlso DS.Tables(0).Rows.Count > 0 Then
            FormataGrid()
        Else
            Exit Sub
        End If

    End Sub

    Private Sub FormataGrid()
        Dim cont As Integer
        Dim Colunas As New DataGridViewTextBoxColumn()

        With dtGridResultados
            .AutoGenerateColumns = False
            .Columns.Clear()

            For cont = 0 To UBound(Split(prvCampos, ","))

                'define cada coluna do grid
                Dim Col As New DataGridViewTextBoxColumn()
                With Col
                    .DataPropertyName = Mid(Split(prvCampos, ",")(cont).ToString, 2)
                    .HeaderText = Split(prvDescricoes, ",")(cont).ToString
                    .Width = CInt(Split(prvLarguras, ",")(cont).ToString)
                    .Frozen = False
                    .ReadOnly = True
                    .FillWeight = 100
                    .AutoSizeMode = DataGridViewAutoSizeColumnMode.NotSet
                    .Visible = True
                End With
                .Columns.Add(Col)
                .Refresh()
            Next

            .DataSource = dsPesquisa.Tables(0)
            Call CarregarCboCampos()

        End With

    End Sub

#End Region

    Private Sub dtGridResultados_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dtGridResultados.CellClick

        dtGridResultados.CurrentRow.Selected = True

    End Sub
    Private Sub dtGridResultados_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dtGridResultados.CellDoubleClick
        prvId = CLng(dtGridResultados.Rows(e.RowIndex).Cells(0).Value.ToString())
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
    End Sub

    Private Sub frmPesquisa_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If (e.KeyCode = Keys.Enter) Then
            With dtGridResultados
                If (.SelectedRows.Count = 1) Then
                    For cont As Integer = 0 To .Rows.Count - 1
                        If (.Rows(cont).Selected) Then
                            prvId = CLng(dtGridResultados.Rows(cont).Cells(0).Value.ToString())
                            Me.DialogResult = System.Windows.Forms.DialogResult.OK
                            Exit For
                            Me.Close()
                        End If
                    Next
                End If
            End With
        End If

    End Sub
    Private Sub frmPesquisa_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.Text = prvTitulo
        txtCampo.Select()
    End Sub
    Private Sub cboCampos_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboCampos.TextChanged

        If (Mid(Split(prvCampos, ",")(cboCampos.SelectedIndex), 1, 1) = "s") Then
            txtCampo.TipoTexto = OTHTextBox.OTHTextBox.eTipoTexto.Normal
        ElseIf (Mid(Split(prvCampos, ",")(cboCampos.SelectedIndex), 1, 1) = "i") Then
            txtCampo.TipoTexto = OTHTextBox.OTHTextBox.eTipoTexto.Pesquisa
        End If

    End Sub
    Private Sub txtCampo_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtCampo.TextChanged
        Dim view As New DataView(DS.Tables(0))

        Try
            If (txtCampo.Text <> String.Empty) Then
                If (Mid(Split(prvCampos, ",")(cboCampos.SelectedIndex), 1, 1) = "s") Then
                    view.RowFilter = Mid(Split(prvCampos, ",")(cboCampos.SelectedIndex), 2) & " like '" & txtCampo.Text & "%'"
                    dtGridResultados.DataSource = view
                ElseIf (Mid(Split(prvCampos, ",")(cboCampos.SelectedIndex), 1, 1) = "i") Then
                    view.RowFilter = Mid(Split(prvCampos, ",")(cboCampos.SelectedIndex), 2) & " in(" & txtCampo.Text & ")"
                    dtGridResultados.DataSource = view
                End If
            Else
                view.RowFilter = ""
                dtGridResultados.DataSource = view
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, Me.Text)
        End Try
    End Sub
End Class